//
//  CommentView.m
//  Cycling
//
//  Created by 迟国强 on 2023/4/18.
//

#import "CommentView.h"

@implementation CommentView

+ (CommentView *)CommentWith:(UIViewController *)vc Complection:(void(^)(NSString *content))block {
    CommentView *commentView = [[CommentView alloc] initWithFrame:UIScreen.mainScreen.bounds];
    commentView.backgroundColor = rgba(0, 0, 0, 0.5);
    [commentView initUI];
    commentView.complectionBlock = block;
    [[NSNotificationCenter defaultCenter] addObserver:commentView selector:@selector(keyBoardWillShow:)name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:commentView selector:@selector(keyBoardWillHide:)name:UIKeyboardWillHideNotification object:nil];
    
    NSLog(@"%@", NSObject.getKeyWindowRootViewController);
    [NSObject.getKeyWindowRootViewController gq_presentLikeAlertView:commentView];
    NSObject.getKeyWindowRootViewController.gq_enableBlankDismiss = YES;
    return commentView;
}

-(void)keyBoardWillShow:(NSNotification*)note{
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
    CGRect keyBoardBounds = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyBoardHeight = keyBoardBounds.size.height;
    CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    self.view.center = CGPointMake(self.ce_width/2, self.ce_height - 200);
}

-(void)keyBoardWillHide:(NSNotification*)note{
    self.view.center = self.center;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [NSObject.getKeyWindowRootViewController gq_removeBgView];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)initUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 42, 330)];
    view.userInteractionEnabled = YES;
    view.backgroundColor = UIColor.whiteColor;
    view.layer.cornerRadius = 28;
    view.layer.masksToBounds = YES;
    view.center = self.center;
    [self addSubview:view];
    self.view = view;
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = CycLocalizedString(@"Your comment", @"请对路线进行评论");
    titleL.textColor = rgbHex(0x303030, 1.0);
    titleL.font = PingFangSemiboldFont(22);
    titleL.textAlignment = NSTextAlignmentCenter;
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_top).offset(30);
            make.centerX.equalTo(self.mas_centerX);
    }];
    
    UITextView *textView = [[UITextView alloc] init];
    textView.delegate = self;
    textView.text = CycLocalizedString(@"Your opinion", @"请输入您的看法！");
    textView.textColor = rgbHex(0xB3B3B3, 1);
    textView.font = PingFangFont(20);
    textView.layer.cornerRadius = 6;
    textView.layer.borderWidth = 1;
    textView.layer.borderColor = rgbHex(0xE5E5E5, 1.0).CGColor;
    textView.layer.masksToBounds = YES;
    [view addSubview:textView];
    self.textView = textView;
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleL.mas_bottom).offset(30);
        make.left.equalTo(view.mas_left).offset(16);
        make.right.equalTo(view.mas_right).offset(-16);
        make.bottom.equalTo(view.mas_bottom).offset(-80);

    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = rgba(0, 0, 0, 0.15);
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left);
        make.right.equalTo(view.mas_right);
        make.bottom.equalTo(view.mas_bottom).offset(-58);
        make.height.offset(1);
    }];
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBtn setTitle:CycLocalizedString(@"Comments", @"评论") forState:UIControlStateNormal];
    [submitBtn setTitleColor:rgb(10, 211, 159) forState:UIControlStateNormal];
    submitBtn.titleLabel.font = PingFangMediumFont(24);
    [submitBtn addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(15);
        make.right.equalTo(view.mas_right).offset(-15);
        make.bottom.equalTo(view.mas_bottom);
        make.height.offset(57);
    }];
}

- (void)sure{
    self.complectionBlock(self.textView.text);
}
- (void)cancle{
    self.complectionBlock(@"");
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    textView.textColor = rgbHex(0x303030, 1);
    if ([textView.text isEqualToString:CycLocalizedString(@"Your opinion", @"请输入您的看法！")]) {
        textView.text = @"";
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
    
}

- (void)textViewDidChangeSelection:(UITextView *)textView {
    
}
@end
